﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Collections;
using System.IO;
using System.Text;

using Web.SystemConfig.ArchiveCode;
using ePlanet.ArchiveMange.Models;
using ePlanet.ArchiveMange.Services;
using DreamBuilder.Core.Container;
using Microsoft.Practices.Unity;
using ePlanet.ArchiveMange.Enum;
using CommonModule;
using ePlanet.SingleWell.Services;
using ePlanet.SingleWell.Models;

namespace Web.Views.ArchiveFlowView
{
    public partial class PrintBoxCatagoryView : PageBase
    {
        private const string _pageBreakHTML = @"<hr align='center' width='100%' size='1' noshade style='display:none;' />
    <div style='page-break-after:always;'></div>";

        private const string _rowItemFmt = @"<tr><td style='text-align:center;'>{0}</td><td style='text-align:center;'>{1}</td><td style='text-align:center;'>{2}</td><td class='cellPadding'>{3}</td><td style='text-align:center;'>{4}</td><td style='text-align:center;'>{5}</td><td style='text-align:center;'>{6}</td></tr>";

        private const int _pageSize = 15;

        [Dependency]
        public IArchiveMenuService ArchiveMenuService
        {
            get;
            set;
        }

        private int _baseArchiveTypeId;

        public int BaseArchiveTypeId
        {
            get
            {
                if (_baseArchiveTypeId == 0)
                {
                    _baseArchiveTypeId = ArchiveMenuService.GetArchiveBaseIDById(int.Parse(Request.QueryString["archiveType"]));
                }
                return _baseArchiveTypeId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            WriteUserLog(string.Format("用户{0}查看了{1}类型档案的企业目录", CurrentUser.Real_Name, BaseArchiveTypeId), OracleLogType.ArchiveManage);
            DataTable archiveTable = null;
            ArrayList idList = null;
            if (Request.QueryString["from"].Equals("dazl"))     //信息中心-E6
            {
                idList = Session["dazl_hnml_selectedkeys"] as ArrayList;
            }
            else if (Request.QueryString["from"].Equals("dasj")) //档案上交 基层-信息中心
            {
                idList = Session["dasj_hnml_selectedkeys"] as ArrayList;
            }
            else if (Request.QueryString["from"].Equals("sjgl")) //数据管理
            {
                idList = Session["sjgl_hnml_selectedkeys"] as ArrayList;
            }
            List<string> myIdList = new List<string>();
            foreach (var id in idList)
            {
                myIdList.Add(id.ToString());
            }
            switch (BaseArchiveTypeId)
            {
                //文书类
                case ((int)ArchiveBaseType.Document):
                    IDocumentArchiveService documentArchiveService = UContainer.Resolve<IDocumentArchiveService>();
                    archiveTable = documentArchiveService.GetDocumentArchiveDataByIds(myIdList);
                    break;
                //会计类
                case ((int)ArchiveBaseType.Account):
                    IAccountArchiveService accountArchiveService = UContainer.Resolve<IAccountArchiveService>();
                    archiveTable = accountArchiveService.GetAccountArchiveDataByIds(myIdList);
                    break;
                //基建类
                case ((int)ArchiveBaseType.BulidProject):
                    IBuildProjectArchiveService buildProjectService = UContainer.Resolve<IBuildProjectArchiveService>();
                    archiveTable = buildProjectService.GetBuildProjectArchiveDataByIds(myIdList);
                    break;
                //设备类
                case ((int)ArchiveBaseType.Equipment):
                    IEquipmentService equipService = UContainer.Resolve<IEquipmentService>();
                    archiveTable = equipService.GetEquipmentArchiveDataByIds(myIdList);
                    break;
                //油气勘探类
                case ((int)ArchiveBaseType.OliSearch):
                    IOilSearchArchiveService oliSearchService = UContainer.Resolve<IOilSearchArchiveService>();
                    archiveTable = oliSearchService.GetOilSearchArchiveDataByIds(myIdList);
                    break;
                //音像类
                case ((int)ArchiveBaseType.AudioVedio):
                    IAudioArchiveService audioService = UContainer.Resolve<IAudioArchiveService>();
                    archiveTable = audioService.GetAudioArchiveDataByIds(myIdList);
                    break;
                //实物类
                case ((int)ArchiveBaseType.RealKind):
                    IRealKindArchiveService realService = UContainer.Resolve<IRealKindArchiveService>();
                    archiveTable = realService.GetRealKindArchiveDataByIds(myIdList);
                    break;
                //科研类
                case ((int)ArchiveBaseType.Technology):
                    ITechArchiveService techService = UContainer.Resolve<ITechArchiveService>();
                    archiveTable = techService.GetTechArchiveDataByIds(myIdList);
                    break;
                //光盘类
                case ((int)ArchiveBaseType.CDKind):
                    ICDArchiveService cdService = UContainer.Resolve<ICDArchiveService>();
                    archiveTable = cdService.GetCDArchiveDataByIds(myIdList);
                    break;
            }

            string fmtHTML = File.ReadAllText(
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Views\ArchiveFlowView\PrintBoxCatagoryTemplate.htm"));

            if (string.IsNullOrEmpty(fmtHTML.Trim()))
            {
                Alert("缺少文件！无法打印！");
                return;
            }

            StringBuilder sb = new StringBuilder();
            //sb.Append(_pageBreakHTML);
            List<string> trTextList = new List<string>();
            int i = 0;
            int pageIndex = 1;
            int rowIndex = 0;
            foreach (DataRow row in archiveTable.Rows)
            {
                i++;
                rowIndex++;

                trTextList.Add(string.Format(_rowItemFmt, rowIndex, row["filenum"], row["publisher"],
                    row["title"], row["publishtime"], row["pagenum"], row["remark"]));

                if (i == _pageSize)
                {
                    sb.Append(fmtHTML.Replace("{ITEMS}", string.Join(" ", trTextList.ToArray())).Replace("{PAGE}", pageIndex.ToString()));
                    trTextList.Clear();
                    sb.Append(_pageBreakHTML);
                    if (pageIndex > 1) sb.Append("<br />");
                    i = 0;

                    pageIndex++;
                }
            }

            if (trTextList.Count() > 0)
            {
                int iCount = trTextList.Count();
                while (iCount < 15)
                {
                    trTextList.Add(string.Format(_rowItemFmt, string.Empty, string.Empty, string.Empty,
                        string.Empty, string.Empty, string.Empty, string.Empty));
                    iCount = trTextList.Count();
                }
                sb.Append(fmtHTML.Replace("{ITEMS}", string.Join(" ", trTextList.ToArray())).Replace("{PAGE}", pageIndex.ToString()));
                trTextList.Clear();
                //sb.Append(_pageBreakHTML);
            }

            string printText = File.ReadAllText(Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory, @"Views\ArchiveFlowView\PrintBoxCatagoryTemplateFrame.htm"));

            Session["hnmlPrint"] = printText.Replace("{content}", sb.ToString());
            ClientScript.RegisterStartupScript(this.GetType(), "print",
                "<script type='text/javascript' > window.open('PageForPrint.aspx?from=hnml','newpage'); </script>");
        }
    }
}